package jdbc.day2.demo4_对象工厂;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class ObjectFactory {
    //key是对象的名字，value是实例
    private static Map<String,Object> map=new HashMap<>();
    static {
        BufferedReader br=null;
        try{
            br=new BufferedReader(new InputStreamReader(ObjectFactory.class.getClassLoader().getResourceAsStream("obj.txt"),"utf8"));
            String s;
            while ((s=br.readLine())!=null){
                System.out.println(s);
                String[]arr=s.trim().split("=");
                Class c=Class.forName(arr[1]);
                Object obj=c.newInstance();
                map.put(arr[0],obj);
            }
            br.close();
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取对象工厂文件出错");
        }
    }

    public static Object getObject(String key){
        return map.get(key);
    }
}
